require "highline" 

require "wondercap/lib/multi_configuration/multi_configuration"
require "wondercap/lib/multi_configuration/files_reader"
require "wondercap/lib/utils/logging"

namespace :wondercap do

  class << self
    include Wondercap::Logging
    
    attr_reader :ui, :mc
  end
  
  task :init_highline do
    @ui = HighLine.new
  end
  
  task :init_multi_configuration do
    @mc = Wondercap::MultiConfiguration.new
    @mc_files_reader = reader = Wondercap::FilesReader.new(mc, self.top)
    class << @mc
      self
    end.class_eval do
      define_method :search_paths do
        reader.paths
      end
    end
  end
  
  task :fill_search_paths do
    @mc.search_paths << "/usr/local/wondercap/extensions/" << "/etc/wondercap.rb" << "/etc/wondercap.mcap" << "/usr/local/etc/wondercap.rb" << "/usr/local/etc/wondercap.mcap" << "/usr/local/wondercap/conf/"
  end
  
  task :process_multi_configuration_files do
    @mc_files_reader.process    
  end
    
  on :start do
    init_highline
    init_multi_configuration
    fill_search_paths
    process_multi_configuration_files
  end
  
  desc "List all available configurations"
  task :list_configurations do
    ui.say ui.list(mc.map { |k,v| k.to_s }, :rows)
  end

  desc "List all tasks in all available configurations"
  task :list_tasks do
    mc.each do |k,v|
      ui.say "#{k.to_s} tasks:"
      
      tasks = v.task_list(true)
      unless tasks.empty?
        ui.say ui.list(tasks.map { |task| "{task.fully_qualified_name}" }.sort, :columns_across)
      else
        ui.say "none"
      end
      ui.say "\n"
    end
  end

end